package jsyf.oa.web.capital;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.capital.BankDebitsFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by houtailei on 2018/2/25.
 */
@Api(value = "T_CPT_BANK_DEBITS_LEDGER接口", description = "T_CPT_BANK_DEBITS_LEDGER接口")
@Controller
@RequestMapping("/bankDebitsFacade")
public class BankDebitsController {
    @Resource
    private BankDebitsFacade bankDebitsFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.bankDebitsFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject resultId = this.bankDebitsFacade.getById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(resultId));
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.bankDebitsFacade.delById(jsonData);
        ResponseUtils.putJsonResponse(response, result);
    }


    @ResponseBody
    @RequestMapping(value = "/getBankDebitsByCompanyId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "汇票台账列表",
            notes = " 汇票台账列表  ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void getBankDebitsByCompanyId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = this.bankDebitsFacade.getBankDebitsByCompanyId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/getBankDebitsByReport.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "汇票台账列表",
            notes = " 汇票台账列表  ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{companyId:'201712161613432EAF27DBCCB7D0E6CA',date:'2018-03-05'}", dataType = "String", paramType = "body")
    public void getBankDebitsByReport(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.bankDebitsFacade.getBankDebitsByReport(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }

    @RequestMapping(value = "/getBankDebitsByDownload.json", method = RequestMethod.GET)
    @ApiOperation(
            value = "汇票台导出列表",
            notes = " 汇票台账导出列表  ",
            httpMethod = "GET",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void getBankDebitsByDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam String companyId, @RequestParam String debitsNo, @RequestParam String comeProject, @RequestParam String dueDate,
                                        @RequestParam String endorseFlag, @RequestParam String endorseDate, @RequestParam String key) throws Exception {
        List<JSONObject> records = this.bankDebitsFacade.getBankDebitsByDownload(companyId, debitsNo, comeProject, dueDate, endorseFlag, endorseDate, key);
        getDownExcel(records, response);
    }

    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response) throws Exception {
        //导出表名、列名
        String title = "汇票台账列表";
        String[] rowsName = new String[]{"汇票号码", "到期日期", "金额", "收票日期", "出票人", "出票银行",
                "收入项目", "出票日期", "是否背书", "背书日期",
                "背书方式", "贴现银行", "贴现息", "贴现金额", "托收银行", "托收是否到账",
                "托收到账日期", "客户名称", "是否质押", "录入人", "制单日期", "支出项目", "备注"};

        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.get("debitsNo");
            objs[1] = jsonObject.get("dueDate");
            objs[2] = jsonObject.get("money");
            objs[3] = jsonObject.get("invoiceDate");
            objs[4] = jsonObject.get("drawerName");
            objs[5] = jsonObject.get("drawerBank");
            objs[6] = jsonObject.get("incomeProject");
            objs[7] = jsonObject.get("drawerDate");
            objs[8] = "1".equals(jsonObject.get("endorseFlag").toString()) ? "是" : "否";
            objs[9] = jsonObject.get("endorseDate");
            if ("0".equals(jsonObject.get("endorseMode"))) {
                objs[10] = "托收";
            } else if ("1".equals(jsonObject.get("endorseMode"))) {
                objs[10] = "贴现";
            } else {
                objs[10] = "转出";
            }
            objs[11] = jsonObject.get("discountBank");
            objs[12] = jsonObject.get("discountInterest");
            objs[13] = jsonObject.get("discountMoney");
            objs[14] = jsonObject.get("bankName");
            objs[15] = "1".equals(jsonObject.get("collectionArriveFlag").toString()) ? "是" : "否";
            objs[16] = jsonObject.get("collectionArriveDate");
            objs[17] = jsonObject.get("customerName");
            objs[18] = "1".equals(jsonObject.get("impawnFlag").toString()) ? "是" : "否";
            objs[19] = jsonObject.get("inputPerson");
            objs[20] = jsonObject.get("createTime");
            objs[21] = jsonObject.get("outcomeProject");
            objs[22] = jsonObject.get("remark");
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
    }
}
